package com.ruoyi.project.business.schedefine.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.project.business.schedefine.entity.ScheduleDefine;
import com.ruoyi.project.business.schedefine.mapper.ScheduleDefineMapper;
import com.ruoyi.project.business.schedefine.request.ListAllScheduleDefineRequest;
import com.ruoyi.project.business.schedefine.response.ScheduleDefineResponse;
import com.ruoyi.project.business.schedefine.service.IScheduleDefineService;
import com.ruoyi.project.system.entity.SysDept;
import com.ruoyi.project.system.service.ISysDeptService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 班次定义Service业务层处理
 * 
 * @author ${author}
 * @date 2024-02-15
 */
@Service
public class ScheduleDefineServiceImpl implements IScheduleDefineService {
    @Autowired
    private ScheduleDefineMapper configScheDefineMapper;
    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询班次定义
     * 
     * @param id 班次定义ID
     * @return 班次定义
     */
    @Override
    public ScheduleDefine selectConfigScheDefineById(Long id) {
        return configScheDefineMapper.selectConfigScheDefineById(id);
    }

    /**
     * 查询班次定义列表
     * 
     * @param param 班次定义
     * @return 班次定义
     */
    @Override
    public List<ScheduleDefine> selectConfigScheDefineList(ScheduleDefine param, Long tenantId) {
        param.setTenantId(tenantId);
        return configScheDefineMapper.selectConfigScheDefineList(param);
    }

    /**
     * 查询班次定义列表
     * 根据部门 id 查询定义的班次， 包含子部门的班次
     *
     * @param param 班次定义
     * @return 班次定义
     */
    @Override
    public List<ScheduleDefine> selectConfigScheDefineListWithChildDept(ScheduleDefine param, Long tenantId) {
        param.setTenantId(tenantId);
        List<SysDept> depts = sysDeptService.selectChildsListByDeptId(param.getDeptId(), tenantId);
        if(CollectionUtils.isEmpty(depts)) {
            return new ArrayList<>();
        }
        List<Long> deptIds = depts.stream().map(SysDept::getDeptId).distinct().collect(Collectors.toList());
//         根据 deptIds 查询
        return configScheDefineMapper.selectListByDeptIds(deptIds, tenantId);
    }

    /**
     * 新增班次定义
     * 
     * @param configScheDefine 班次定义
     * @return 结果
     */
    @Override
    public int insertConfigScheDefine(ScheduleDefine configScheDefine, Long operatorId, Long tenantId) {
        configScheDefine.setCreateTime(DateUtils.getNowDate());
        configScheDefine.setTenantId(tenantId);
        configScheDefine.setCreator(operatorId);
        return configScheDefineMapper.insertConfigScheDefine(configScheDefine);
    }

    /**
     * 修改班次定义
     * 
     * @param configScheDefine 班次定义
     * @return 结果
     */
    @Override
    public int updateConfigScheDefine(ScheduleDefine configScheDefine, Long operatorId, Long tenantId) {
        configScheDefine.setUpdateTime(DateUtils.getNowDate());
        configScheDefine.setTenantId(tenantId);
        configScheDefine.setUpdater(operatorId);
        return configScheDefineMapper.updateConfigScheDefine(configScheDefine);
    }

    /**
     * 批量删除班次定义
     * 
     * @param ids 需要删除的班次定义ID
     * @return 结果
     */
    @Override
    public int deleteConfigScheDefineByIds(Long[] ids, Long operatorId, Long tenantId) {
        return configScheDefineMapper.deleteConfigScheDefineByIds(ids);
    }

    /**
     * 删除班次定义信息
     * 
     * @param id 班次定义ID
     * @return 结果
     */
    @Override
    public int deleteConfigScheDefineById(Long id, Long operatorId, Long tenantId) {
        return configScheDefineMapper.deleteConfigScheDefineById(id);
    }

    @Override
    public List<ScheduleDefineResponse> selectAllOfDeptId(ListAllScheduleDefineRequest request, Long tenantId) {
        ScheduleDefine param = new ScheduleDefine();
        param.setTenantId(tenantId);
        param.setDeptId(request.getDeptId());

        List<ScheduleDefine> configScheDefines = configScheDefineMapper.selectConfigScheDefineList(param);
        List<ScheduleDefineResponse> responses = BeanUtils.copyList(configScheDefines, ScheduleDefineResponse.class);
        return responses;
    }
}
